package com.czkj.hwz.decorate.Activity;

import android.content.Context;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.czkj.hwz.decorate.IMP.HttpDoImp;
import com.czkj.hwz.decorate.R;
import com.czkj.hwz.decorate.adapter.GoodsAdapter;
import com.czkj.hwz.decorate.base.BaseActivity;
import com.czkj.hwz.decorate.base.HttpUrl;
import com.czkj.hwz.decorate.bean.Enterprise;
import com.czkj.hwz.decorate.bean.Goods;
import com.czkj.hwz.decorate.bean.MyTab;
import com.czkj.hwz.decorate.control.GoodsGet;
import com.czkj.hwz.decorate.control.IndexGet;
import com.joanzapata.android.BaseAdapterHelper;
import com.joanzapata.android.QuickAdapter;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;

@ContentView(R.layout.activity_classify)
public class ClassifyActivity extends BaseActivity implements HttpDoImp {
	//activity 必须继承BaseActivity
   // private static final String ARG_PARAM1 = "param1";
   // private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
 //   private String mParam1;
  //  private String mParam2;
    private IndexGet indexGet;
    private ArrayList<Enterprise> mData;
    private ArrayList<Goods> mGoods;
    GoodsGet mGoodsGet;
    private Context mContext;
    private GoodsAdapter goodsAdapter;

    @ViewInject(R.id.ll)
    LinearLayout ll;
    @ViewInject(R.id.ll_h)
    LinearLayout ll_h;
    @ViewInject(R.id.tv_qb)
    TextView tv_qb;
    @ViewInject(R.id.gv_qb)
    GridView gv_qb;
    MyTab mMyTab_v,mMyTab_h;
    private int H_index=0,V_index=-1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
		//这个是fragment中，不使用在activity中；
       // super.onCreate(savedInstanceState);
       // if (getArguments() != null) {
     //       mParam1 = getArguments().getString(ARG_PARAM1);
      //      mParam2 = getArguments().getString(ARG_PARAM2);
     //   }
	 //这个是activity中的，你可以新建个activiyt看下，基本上是没有变化固定的。
	 super.onCreate(savedInstanceState);
//        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//        setSupportActionBar(toolbar);
	
		//获取数据放在oncreate中，如果是每次页面展示的时候，都要获取的话，放到 onResume里面；
	      indexGet=new IndexGet(ClassifyActivity.this,this);
          indexGet.getIndex(null);
	 
    }
	//都是fragment中，不需要
  //  @Override
   // public void onViewCreated(View view, Bundle savedInstanceState) {
  //      super.onViewCreated(view, savedInstanceState);
   // }

  //  @Override
  //  protected void lazyLoad() {
  //      indexGet=new IndexGet(getContext(),this);
  //      indexGet.getIndex(null);
  //  }

    @Override
    public void onSuccess(String flag, Object... objects) {
        switch (flag) {
            case "indexGet":
                mData= (ArrayList<Enterprise>) objects[2];
                mMyTab_h=new MyTab(ClassifyActivity.this,LinearLayout.HORIZONTAL) {
                    @Override
                    public void onItemClickListener(int poistion) {
                        int id=mData.get(poistion).getId();
                        mGoodsGet=new GoodsGet(ClassifyActivity.this,ClassifyActivity.this);
                        HashMap<String, String> reqParams = new HashMap<>();
                        reqParams.put("enterpriseid", String.valueOf(id));
                        mGoodsGet.getList(reqParams);
                    }
                };
                mMyTab_h.setData(mData);
                ll_h.addView(mMyTab_h.getView());
                break;
            case "getList":
                mGoods= (ArrayList<Goods>) objects[1];
                //goodsAdapter=new GoodsAdapter(getContext(),mGoods);
                //gv_qb.setAdapter(goodsAdapter);
                gv_qb.setAdapter(new QuickAdapter<Goods>(ClassifyActivity.this,R.layout.grideview_goods_item,mGoods) {
                    @Override
                    protected void convert(BaseAdapterHelper helper, Goods item) {
                        helper.setText(R.id.tv_title,item.getName());
                        ImageView img = helper.getView(R.id.img);
                        x.image().bind(img, HttpUrl.BASE_URL+item.getSmallImg());
                    }
                });
                break;
        }
    }

    @Override
    public void onError(String errinfo) {

    }

}
